{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "google",
   "metadata": {},
   "source": [
    "##### Copyright 2025 Google LLC."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "apache",
   "metadata": {},
   "source": [
    "Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "you may not use this file except in compliance with the License.\n",
    "You may obtain a copy of the License at\n",
    "\n",
    "    http://www.apache.org/licenses/LICENSE-2.0\n",
    "\n",
    "Unless required by applicable law or agreed to in writing, software\n",
    "distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "See the License for the specific language governing permissions and\n",
    "limitations under the License.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "basename",
   "metadata": {},
   "source": [
    "# set_covering_skiena"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "link",
   "metadata": {},
   "source": [
    "<table align=\"left\">\n",
    "<td>\n",
    "<a href=\"https://colab.research.google.com/github/google/or-tools/blob/main/examples/notebook/contrib/set_covering_skiena.ipynb\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/colab_32px.png\"/>Run in Google Colab</a>\n",
    "</td>\n",
    "<td>\n",
    "<a href=\"https://github.com/google/or-tools/blob/main/examples/contrib/set_covering_skiena.py\"><img src=\"https://raw.githubusercontent.com/google/or-tools/main/tools/github_32px.png\"/>View source on GitHub</a>\n",
    "</td>\n",
    "</table>"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "doc",
   "metadata": {},
   "source": [
    "First, you must install [ortools](https://pypi.org/project/ortools/) package in this colab."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "install",
   "metadata": {},
   "outputs": [],
   "source": [
    "%pip install ortools"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "description",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "  Set covering in Google CP Solver.\n",
    "\n",
    "  Example from Steven Skiena, The Stony Brook Algorithm Repository\n",
    "  http://www.cs.sunysb.edu/~algorith/files/set-cover.shtml\n",
    "  '''\n",
    "  Input Description: A set of subsets S_1, ..., S_m of the\n",
    "  universal set U = {1,...,n}.\n",
    "\n",
    "  Problem: What is the smallest subset of subsets T subset S such\n",
    "  that \\cup_{t_i in T} t_i = U?\n",
    "  '''\n",
    "  Data is from the pictures INPUT/OUTPUT.\n",
    "\n",
    "  Compare with the following models:\n",
    "  * MiniZinc: http://www.hakank.org/minizinc/set_covering_skiena.mzn\n",
    "  * Comet: http://www.hakank.org/comet/set_covering_skiena.co\n",
    "  * ECLiPSe: http://www.hakank.org/eclipse/set_covering_skiena.ecl\n",
    "  * SICStus Prolog: http://www.hakank.org/sicstus/set_covering_skiena.pl\n",
    "  * Gecode: http://hakank.org/gecode/set_covering_skiena.cpp\n",
    "\n",
    "\n",
    "  This model was created by Hakan Kjellerstrand (hakank@gmail.com)\n",
    "  Also see my other Google CP Solver models:\n",
    "  http://www.hakank.org/google_or_tools/\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "code",
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.constraint_solver import pywrapcp\n",
    "\n",
    "\n",
    "def main():\n",
    "\n",
    "  # Create the solver.\n",
    "  solver = pywrapcp.Solver('Set covering Skiena')\n",
    "\n",
    "  #\n",
    "  # data\n",
    "  #\n",
    "  num_sets = 7\n",
    "  num_elements = 12\n",
    "  belongs = [\n",
    "      # 1 2 3 4 5 6 7 8 9 0 1 2  elements\n",
    "      [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],  # Set 1\n",
    "      [0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],  # 2\n",
    "      [0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],  # 3\n",
    "      [0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0],  # 4\n",
    "      [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0],  # 5\n",
    "      [1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 1, 0],  # 6\n",
    "      [0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1]  # 7\n",
    "  ]\n",
    "\n",
    "  #\n",
    "  # variables\n",
    "  #\n",
    "  x = [solver.IntVar(0, 1, 'x[%i]' % i) for i in range(num_sets)]\n",
    "\n",
    "  # number of choosen sets\n",
    "  z = solver.IntVar(0, num_sets * 2, 'z')\n",
    "\n",
    "  # total number of elements in the choosen sets\n",
    "  tot_elements = solver.IntVar(0, num_sets * num_elements)\n",
    "\n",
    "  #\n",
    "  # constraints\n",
    "  #\n",
    "  solver.Add(z == solver.Sum(x))\n",
    "\n",
    "  # all sets must be used\n",
    "  for j in range(num_elements):\n",
    "    s = solver.Sum([belongs[i][j] * x[i] for i in range(num_sets)])\n",
    "    solver.Add(s >= 1)\n",
    "\n",
    "  # number of used elements\n",
    "  solver.Add(tot_elements == solver.Sum([\n",
    "      x[i] * belongs[i][j] for i in range(num_sets) for j in range(num_elements)\n",
    "  ]))\n",
    "\n",
    "  # objective\n",
    "  objective = solver.Minimize(z, 1)\n",
    "\n",
    "  #\n",
    "  # search and result\n",
    "  #\n",
    "  db = solver.Phase(x, solver.INT_VAR_DEFAULT, solver.INT_VALUE_DEFAULT)\n",
    "\n",
    "  solver.NewSearch(db, [objective])\n",
    "\n",
    "  num_solutions = 0\n",
    "  while solver.NextSolution():\n",
    "    num_solutions += 1\n",
    "    print('z:', z.Value())\n",
    "    print('tot_elements:', tot_elements.Value())\n",
    "    print('x:', [x[i].Value() for i in range(num_sets)])\n",
    "\n",
    "  solver.EndSearch()\n",
    "\n",
    "  print()\n",
    "  print('num_solutions:', num_solutions)\n",
    "  print('failures:', solver.Failures())\n",
    "  print('branches:', solver.Branches())\n",
    "  print('WallTime:', solver.WallTime(), 'ms')\n",
    "\n",
    "\n",
    "main()\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
